<?php

class ProductAdv extends CWidget
{

    public $model;
    public $has_adv = false;
    protected $_activeTab = false;
    protected $_renderTab;

    public function init()
    {
	if (!$this->model) {
	    return false;
	}
	$images = $this->model->getImages();
	$videos = $this->model->getVideos();
	$maps = $this->model->getMaps();
	if ($images || $videos || $maps) {
	    $this->has_adv = true;	    
	}
	
    }

    public function run()
    {
	if (!$this->has_adv) {
	    return false;
	}
	$display = $this->makeTabs();
	$display .= $this->makeAdv();
	echo $display;
    }

    public function getStartTabTags($tabType)
    {
	$active = $this->_activeTab ? '' : 'active';
	$this->_activeTab = true;
	return array(
	    CHtml::openTag('li', array('class' => 'floatL ' . $active, 'id' => 'tab' . ucfirst($tabType), 'onclick' => 'setTab("' . $tabType . '", this)')),
	    CHtml::openTag('a', array('href' => 'javascript:void(0)'))
	);
    }

    public function getEndTabTags()
    {
	return array(
	    CHtml::closeTag('a'),
	    CHtml::closeTag('li')
	);
    }

    public function getTab(array $newTab, $tabType)
    {
	if (!$newTab) {
	    return false;
	}

	$tab = array_merge($this->getStartTabTags($tabType), $newTab, $this->getEndTabTags());
	return implode('', $tab);
    }

    public function makeAdv()
    {
	$display = $this->renderImages();
	$display .= $this->renderVideos();
	$display .= $this->renderMap();
	if (!empty($display)) {
	    $display = '<div class="recordAdv radius5">' . $display . '</div>';
	}

	return $display;
    }

    public function renderVideos()
    {
	$display = '';
	$videos = $this->model->getVideos();
	if (!$videos) {
	    return $display;
	}
	$hideClass = $this->_renderTab == 'video' ? '' : 'hide';
	$modelClass = $this->model instanceof OpinionModel ? 'opinionVideo' : '';
	$display = '<div class="videos advContent ' . $hideClass . '" id="advVideo"><div class="' . $modelClass . '"  id="advVideos">';
	foreach ($videos as $video) {
	    $player = $this->widget('ext.yiitube.Yiitube', array('v' => $video->getDbData('path'), 'player' => $video->getDbData('url_type')), true);
	    $row = array(
		'<div>',
		$player,
		'</div>'
	    );
	    $display .= implode($row);
	}
	$display .= '</div></div><div class="clear"></div>';
	return $display;
    }

    public function renderMap()
    {
	$display = '';
	$maps = $this->model->getMaps();
	if (!$maps) {
	    return $display;
	}	
	$hideClass = $this->_renderTab == 'map' ? '' : 'hide';
	$modelClass = $this->model instanceof OpinionModel ? 'opinionMap' : 'googleMap';
	$display = '<div class="maps advContent ' . $hideClass . '" id="advMap"><div id="' . $modelClass . '" style="height:350px;"></div><ul class="coordList">';
	$modelMap = $this->model instanceof OpinionModel ? 'opinionMap' : 'map';
	foreach ($maps as $map) {
	    $display .= '<li><a href="javascript:void(0)" onclick="$.data(container, \'' . $modelMap . '\').displayCoords(' . $map->getDbData('latitude') . ', ' . $map->getDbData('longitude') . ')">' . $map->getDbData('address') . '</a></li>';
	}

	$display .= '</ul></div><div class="clear"></div>';
	return $display;
    }

    public function renderImages()
    {
	$display = '';
	$images = $this->model->getImages();
	if (!$images) {
	    return $display;
	}

	$hideClass = $this->_renderTab == 'image' ? '' : 'hide';

	$modelClass = $this->model instanceof OpinionModel ? 'opinionImage' : '';
	$display = '<div class="images advContent ' . $hideClass . '" id="advImage"><div class="' . $modelClass . '"  id="advImages">';	
	foreach ($images as $image) {
	    $row = array(
		'<div>',
		CHtml::openTag('a', array(
		    'href' => $image->getImage(),
		    'rel' => 'prettyPhoto',
		    'title' => $image->getDbData('real_name'),
		    'onclick' => 'return false;'
		)),
		CHtml::openTag('img', array(
		    'src' => $image->getImage(true),
		    'style' => 'vertical-align:middle'
		)),
		'</a>',
		'</div>'
	    );
	    $display .= implode($row);
	}
	$display .= '</div></div><div class="clear"></div>';

	return $display;
    }

    public function makeTabs()
    {
	$display = '<ul class="tabs infoTabs">';
	$images = $this->model->getImages();
	if ($images) {
	    $display .= $this->getTab(array(
		'<i class="icon-picture"></i> ',
		'Фото'), 'image');
	    if (empty($this->_renderTab)) {
		$this->_renderTab = 'image';
	    }
	}
	$videos = $this->model->getVideos();
	if ($videos) {
	    $display .= $this->getTab(array(
		'<i class="icon-facetime-video"></i> ',
		'Видео'), 'video');
	    if (empty($this->_renderTab)) {
		$this->_renderTab = 'video';
	    }
	}
	$maps = $this->model->getMaps();
	if ($maps) {
	    $display .= $this->getTab(array(
		'<i class="icon-map-marker"></i> ',
		'Карта'), 'map');
	    if (empty($this->_renderTab)) {
		$this->_renderTab = 'map';
	    }
	}
	$display .= '</ul>';
	return $display;
    }

}
